<?php
class indexMod extends commonMod{
	
	public function setCookie(){
		setcookie('tk_auth',cp_encode("kfkd#445555554333333slkajc","fendouslsldl"),time()+3600*24*365,'/');
	}
	public function checkAccess(){
		$tk=$_COOKIE['tk_auth'];
		if(isset($tk)){
			if(cp_decode($tk,"fendouslsldl")=="kfkd#445555554333333slkajc"){
				return true;
			}
		}
		return false;
	}
	public function index(){
		if(!$this->checkAccess()){
			echo "can't find it!";
			exit;
		}
		$this->assign("title", "我的密码本");
		$list=$this->model->table("PasswordInfo")->select();
		$this->assign("listData", $list);
		$this->display();
	}

	public function addPasswordGet(){
		if(!$this->checkAccess()){
			echo "can't find it!";
			exit;
		}
		$this->display();
	}

	public function addPasswordPost(){
		if(!$this->checkAccess()){
			echo "can't find it!";
			exit;
		}
		$password=$_POST["Password"];
		if(empty($password)){
			echo "必须输入密码";
			exit();
		}
		$data["Title"]=$_POST["Title"];
		$data["Description"]=$_POST["Description"];
		$data["Content"]=cp_encode("##$####本人密码信息####$##".$_POST["Content"],$password);
		$data["Sort"]=100;
		$data["CreateTime"]=time();
		$this->model->table("PasswordInfo")->data($data)->insert();
		echo "添加成功"."<a href='index'>返回</a>";
	}

	public function edit()
	{
		if(!$this->checkAccess()){
			echo "can't find it!";
			exit;
		}
		$id=$_REQUEST["id"];
		$password=$_REQUEST["Password"];
		if(empty($id)){
			echo "请指定id";
			exit();
		}
		$areadyDecode=cp_decode($_REQUEST["areadyDecode"],"kkcklai*34dJkl2^332");

		if(empty($areadyDecode) || $areadyDecode=="false"){
			$m=$this->model->table("PasswordInfo")->where("id=$id")->find();
			
			if(!empty($password)){
				 $decodeText=cp_decode($m["Content"],$password);

				if(strpos(decodeText, "##$####本人密码信息####$##")!=-1){
					$m["Content"]=str_replace("##$####本人密码信息####$##", "", $decodeText);
					$m["areadyDecode"]=cp_encode("true","kkcklai*34dJkl2^332");
				}else{
					$m["areadyDecode"]=cp_encode("false","kkcklai*34dJkl2^332");
				}
			}
			$this->assign("model", $m );
			$this->display();
		}elseif ($areadyDecode=="true") {
			$password=$_POST["Password"];
			if(empty($password)){
				echo "必须输入密码";
				exit();
			}
			$data["Title"]=$_POST["Title"];
			$data["Description"]=$_POST["Description"];
			$data["Content"]=cp_encode("##$####本人密码信息####$##".$_POST["Content"],$password);
			$this->model->table("PasswordInfo")->data($data)->where("id=$id")->update();
			echo "修改成功"."<a href='index'>返回</a>";
		}
		
	}

	public function show(){
		if(!$this->checkAccess()){
			echo "can't find it!";
			exit;
		}
		$id=$_REQUEST["id"];
		$password=$_REQUEST["Password"];
		if(empty($id)){
			echo "请指定id";
			exit();
		}
		$m=$this->model->table("PasswordInfo")->where("id=$id")->find();
		if(is_null($m) || empty($m)){
			echo "该对象不存在";
			exit();
		}
		if(!empty($password)){
			$m["Content"]=str_replace("##$####本人密码信息####$##", "", cp_decode($m["Content"],$password)) ;
		}
		$this->assign("model", $m );
		$this->display();
	}

}